package com.cn.appcore.ext

import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder


private var lastClickTime: Long = 0

/**
 * 简单的基类Adapter，复杂的请直接继承BRVAH
 */
open class QuickAdapter<T>(layoutResId: Int, convert:(helper: BaseViewHolder, item: T)->Unit) :
    BaseQuickAdapter<T, BaseViewHolder>(layoutResId, mutableListOf()) {

    private var bindViewHolder:(helper: BaseViewHolder, item: T)->Unit = convert

    override fun convert(holder: BaseViewHolder, item: T) {
        bindViewHolder(holder,item)
    }
}

/**
 * 防抖动处理
 */
fun BaseQuickAdapter<*, BaseViewHolder>.setOnFastClickListener(action: (data: MutableList<*>, position: Int) -> Unit) {

    setOnItemClickListener { adapter, _, position ->
        val currentTime = System.currentTimeMillis()
        if (currentTime - lastClickTime > 300L) {
            action.invoke(adapter.data, position)
        } else {
            return@setOnItemClickListener
        }
        lastClickTime = currentTime
    }
}

/**
 * 防抖动处理
 */
fun BaseQuickAdapter<*, BaseViewHolder>.setOnFastChildClickListener(action: (view: View, position: Int) -> Unit) {
    setOnItemChildClickListener { adapter, view, position ->
        val currentTime = System.currentTimeMillis()
        if (currentTime - lastClickTime > 800L) {
            action.invoke(view, position)
        } else {
            return@setOnItemChildClickListener
        }
        lastClickTime = currentTime
    }
}





fun RecyclerView.smoothScrollToEnd() {
    if (adapter != null && adapter!!.itemCount > 0) {
        smoothScrollToPosition(adapter!!.itemCount - 1)
    }
}

fun RecyclerView.scrollToEnd() {
    if (adapter != null && adapter!!.itemCount > 0) {
        scrollToPosition(adapter!!.itemCount - 1)
    }
}



/**
 * 滚动置顶，只支持线性布局
 */
fun RecyclerView.scrollTop(position: Int) {
    if (layoutManager is LinearLayoutManager) {
        (layoutManager as LinearLayoutManager).scrollToPositionWithOffset(position, 0)
    }
}

inline val RecyclerView.orientation
    get() = if (layoutManager == null) -1 else layoutManager.run {
        when (this) {
            is LinearLayoutManager -> orientation
            is GridLayoutManager -> orientation
            is StaggeredGridLayoutManager -> orientation
            else -> -1
        }
    }

/**
 * 添加平分item的分割线
 */
//addItemDecoration(EvenItemDecoration(App.instance.dp2px(10),3))

/**
 * 单选
 */
//class SingleSelectAdapter<T>: BaseQuickAdapter<T, BaseViewHolder>(R.layout.layout_item, mutableListOf()) {
//
//    //选择的位置(-1则代表默认没有选中，0代表默认选中第一个)
//    var selPosition = 0
//    //临时记录上次选择的位置
//    var temp =-1
//
//    override fun convert(holder: BaseViewHolder, item: T) {
//        holder.itemView.isSelected = holder.layoutPosition==selPosition
//        holder.itemView.setOnClickListener {
//            holder.itemView.isSelected =true
//            //将旧的位置保存下来，用于后面把旧的位置颜色变回来
//            temp = selPosition
//            //设置新的位置
//            selPosition = holder.layoutPosition
//            //更新旧位置
//            notifyItemChanged(temp)
//        }
//    }
//}

/**
 * 多选adapter
 */
//class MultiSelectAdapter<T>: BaseQuickAdapter<T, BaseViewHolder>(R.layout.layout_item, mutableListOf()) {
//
//    //用来记录已经勾选的位置(set集合是为了防止放入重复数据)
//    var multiSelectedList = mutableSetOf<Int>()
//
//    override fun convert(holder: BaseViewHolder, item: T) {
//        holder.itemView.isSelected = multiSelectedList.contains(holder.layoutPosition)
//        holder.itemView.setOnClickListener {
//            if (multiSelectedList.contains(holder.layoutPosition)) {
//                multiSelectedList.remove(holder.layoutPosition)
//                holder.itemView.isSelected = false
//            } else {
//                multiSelectedList.add(holder.layoutPosition)
//                holder.itemView.isSelected = true
//            }
//        }
//    }
//
//    /**
//     * 获取选中的列表
//     */
//    fun getSelectList(): MutableSet<Int> {
//        return multiSelectedList
//    }
//}

/**
 * 多选
 */
//interestAdapter.setOnItemClickListener { adapter, view, position ->
//
//    if (interestList.size > position){
//        if (!interestList[position].isSelected){
//            if (selectedList.size >= 10){
//                showMsg("最多选择10个标签")
//                return@setOnItemClickListener
//            }
//        }
//        interestList[position].isSelected = !interestList[position].isSelected
//        interestAdapter.notifyDataSetChanged()
//        notifySelectedTagData()
//    }
//}